package cn.echo.ecc.mq.consumer;


import cn.echo.ecc.enums.GlobalExceptionCode;
import cn.echo.ecc.service.IPayOrderService;
import cn.echo.ecc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "order-timeout-consumer-group",
        topic = "order-timeout-topic",
        selectorExpression = "order-timeout-tag",
        messageModel = MessageModel.BROADCASTING
)
public class OrderTimeOutConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("开始消费课程订单延迟事务消息！");
        // 1.业务校验
        byte[] body = messageExt.getBody();
        AssertUtil.isNotNull(body, GlobalExceptionCode.PARAM_IS_NOT_BULL);
        String orderNo = new String(body, StandardCharsets.UTF_8);
        payOrderService.cancelPayOrder(orderNo);
        log.info("消费课程订单延迟事务消息成功！");
    }
}
